package com.zc.demo.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.zc.demo.model.VerProperties;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;


@RequestMapping("/ver")
//@RestController
@ResponseBody
@Controller
public class VerificationController {

    //创建日志对象
//    private static Logger logger = (Logger) LoggerFactory.getLogger(VerificationController.class);
    private static Logger logger = LoggerFactory.getLogger(VerificationController.class);
    //属性注入
    @Autowired
    private VerProperties verProperties;
    //后端生成验证码
    @RequestMapping("/getcode")
    public void getcode(HttpSession session, HttpServletResponse response){
        //定义图形验证码的长和宽
//        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 50);

        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(verProperties.getWidth(), verProperties.getHeight());

        //图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write("D:\\Java_project\\java_backend_code\\spring_verification_code\\src\\main\\resources\\images\\line.png");
        //输出code
        String code = lineCaptcha.getCode();

        System.out.println("验证码："+code);
        logger.info("日志输出验证码："+code);
        //要把后端生成的验证码带给前端，需要通过session传递
        session.setAttribute(verProperties.getSession().getKey(),code);
        session.setAttribute(verProperties.getSession().getDate(),new Date());
        try {
            //把验证码写到浏览器
            lineCaptcha.write(response.getOutputStream());
            response.setContentType("image/jpeg");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Pragma", "No-cache");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //验证前端的输入验证码
    @RequestMapping("/check")
    public Boolean check(String ver,HttpSession session){
//        System.out.println("用户输入验证码："+ver);
        //验证是否为空
        if(!StringUtils.hasLength(ver)){
            return false;
        }
//        System.out.println(session.getAttribute("code"));
        String code = (String) session.getAttribute(verProperties.getSession().getKey());
        Date date = (Date)session.getAttribute(verProperties.getSession().getDate());
        if(date==null || System.currentTimeMillis()-date.getTime()>(30*1000)){
            //30秒后过期
            return false;
        }
        //判断是否相同
        return ver.equalsIgnoreCase(code);
    }
}
